package org.iwidget.desktop.model;
import org.iwidget.desktop.core.Iwidget;
import org.iwidget.desktop.core.IwidgetRepository;
import org.iwidget.desktop.ui.*;
import java.awt.*;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import org.w3c.dom.Node;
/**
 *
 * @author Muhammad Hakim A
 */

public class ElementUI extends Element
{
    class MyTimerTask extends TimerTask
    {

        public void run()
        {
            triggerEvent("onTimer");
        }

        MyTimerTask()
        {
        }
    }


    public ElementUI()
    {
        height = 0;
        name = "";
        opacity = 100;
        owner = null;
        width = 0;
        tooltip = "";
        visible = true;
        x = 0;
        y = 0;
    }

    public ElementUI(WidgetElement widget)
    {
        super(widget);
        height = 0;
        name = "";
        opacity = 100;
        owner = null;
        width = 0;
        tooltip = "";
        visible = true;
        x = 0;
        y = 0;
    }

    public String getId()
    {
        String id = toString();
        id = Iwidget.replace(id, ".", "_", 0);
        id = Iwidget.replace(id, "@", "_", 0);
        return id;
    }

    public String getCursor()
    {
        return cursor;
    }

    public Cursor getCursorObject()
    {
        return cursorObj;
    }

    /**
     * Set Cursor
     */
    public void setCursor(String string)
    {
        if(cursor == null || !cursor.equals(string))
        {
            cursor = string;
            if(cursor.equals("default"))
                cursorObj = new Cursor(Cursor.DEFAULT_CURSOR);
            else
            if(cursor.equals("cross"))
                cursorObj = new Cursor(Cursor.CROSSHAIR_CURSOR);
            else
            if(cursor.equals("hand"))
                cursorObj = new Cursor(Cursor.HAND_CURSOR);
            else
            if(cursor.equals("move"))
                cursorObj = new Cursor(Cursor.MOVE_CURSOR);
            else
            if(cursor.equals("text"))
                cursorObj = new Cursor(Cursor.TEXT_CURSOR);
            else
            if(cursor.equals("northResize"))
                cursorObj = new Cursor(Cursor.N_RESIZE_CURSOR);
            else
            if(cursor.equals("southResize"))
                cursorObj = new Cursor(Cursor.S_RESIZE_CURSOR);
            else
            if(cursor.equals("eastResize"))
                cursorObj = new Cursor(Cursor.E_RESIZE_CURSOR);
            else
            if(cursor.equals("westResize"))
                cursorObj = new Cursor(Cursor.W_RESIZE_CURSOR);
            else
            if(cursor.equals("northeastResize"))
                cursorObj = new Cursor(Cursor.NE_RESIZE_CURSOR);
            else
            if(cursor.equals("northwestResize"))
                cursorObj = new Cursor(Cursor.NW_RESIZE_CURSOR);
            else
            if(cursor.equals("southeastResize"))
                cursorObj = new Cursor(Cursor.SE_RESIZE_CURSOR);
            else
            if(cursor.equals("southwestResize"))
                cursorObj = new Cursor(Cursor.SW_RESIZE_CURSOR);
            else
            if(cursor.equals("wait"))
            {
                cursorObj = new Cursor(Cursor.WAIT_CURSOR);
            } else
            {
                ImageIcon image = null;
                byte bytes[] = IwidgetRepository.getInstance().getObject(widget.getName(), string);
                try
                {
                    image = new ImageIcon(bytes);
                    cursorObj = Toolkit.getDefaultToolkit().createCustomCursor(image.getImage(), new Point(), string);
                }
                catch(Exception ex)
                {
                    widget.getParent().getDebugFrame().addText("SYSTEM:Error:Could not load cursor:" + widget.getName() + ":" + string);
                    cursorObj = new Cursor(Cursor.DEFAULT_CURSOR);
                }
            }
            if(myComponent != null)
                myComponent.setCursor(cursorObj);
            setChanged();
        }
    }

    public int getHeight()
    {
        return height;
    }

    public void setHeight(int i)
    {
        if(height != i)
        {
            height = i;
            if(myComponent != null)
                myComponent.setSize(width, height);
            setChanged();
        }
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        name = s;
    }

    public int getOpacity()
    {
        return opacity;
    }

    public void setOpacity(int i)
    {
        if(opacity != i)
        {
            opacity = i;
            if(opacity > 100)
                opacity = 100;
            if(opacity < 0)
                opacity = 0;
            setChanged();
        }
    }

    public Object getOwner()
    {
        return owner;
    }

    public void setOwner(Object o)
    {
        if(o instanceof IwidgetScroller)
        {
            IwidgetScroller scroller = (IwidgetScroller)o;
            owner = scroller.getElementData();
        } else
        if(o instanceof String)
        {
            for(int iCtr = 0; iCtr < widget.uiObjects.size(); iCtr++)
            {
                ElementUI obj = (ElementUI)widget.uiObjects.get(iCtr);
                if(obj.getName().equals((String)o))
                    owner = obj;
            }

        } else
        {
            owner = null;
            widget.getParent().getDebugFrame().addText("SYSTEM:Error:Invalid owner type:" + widget.getName() + ":" + o);
        }
    }

    public int getWidth()
    {
        return width;
    }

    public void setWidth(int i)
    {
        if(width != i)
        {
            width = i;
            if(myComponent != null)
                myComponent.setSize(width, height);
            setChanged();
        }
    }

    public String getTooltip()
    {
        return tooltip;
    }

    public void setTooltip(String s)
    {
        if(tooltip == null || !tooltip.equals(s))
        {
            tooltip = s;
            setChanged();
        }
    }

    public boolean getVisible()
    {
        return visible;
    }

    public void setVisible(boolean b)
    {
        if(visible != b)
        {
            visible = b;
            setChanged();
        }
    }

    public int getX()
    {
        return x;
    }

    public void setX(int i)
    {
        if(x != i)
        {
            x = i;
            if(myComponent != null)
                myComponent.setLocation(x, y);
            setChanged();
        }
    }

    public int getY()
    {
        return y;
    }

    public void setY(int i)
    {
        if(y != i)
        {
            y = i;
            if(myComponent != null)
                myComponent.setLocation(x, y);
            setChanged();
        }
    }

    public JComponent getUIComponent()
    {
        return myComponent;
    }

    public void setUIComponent(JComponent myComponent)
    {
        this.myComponent = myComponent;
        if(actions == null)
            actions = new Hashtable();
        if(actions.get("onDragDrop") != null)
            if(myComponent instanceof WidgetImage)
                ((WidgetImage)myComponent).initializeDrop();
            else
            if(myComponent instanceof WidgetText)
                ((WidgetText)myComponent).initializeDrop();
            else
            if(myComponent instanceof WidgetTextArea)
                ((WidgetTextArea)myComponent).initializeDrop();
    }

    public void setAction(String type, String string)
    {
        ActionElement action = new ActionElement();
        action.setType(type);
        action.setScript(string);
        addAction(type, action);
    }

    protected ActionElement addChildAction(String actionType, Node actionNode)
    {
        ActionElement action = new ActionElement();
        action.addChildAction(actionType, actionNode);
        return action;
    }

    public void addAction(String actionType, ActionElement action)
    {
        if(actions == null)
            actions = new Hashtable();
        actions.put(actionType, action);
        if(actionType.equals("onTimer"))
        {
            int interval = action.getIntervalInMilliseconds();
            MyTimerTask myTimerTask = new MyTimerTask();
            widget.getParent().getTimer().schedule(myTimerTask, interval, interval);
        } else
        if(actionType.equals("onDragDrop") && myComponent != null)
            if(myComponent instanceof WidgetImage)
                ((WidgetImage)myComponent).initializeDrop();
            else
            if(myComponent instanceof WidgetText)
                ((WidgetText)myComponent).initializeDrop();
            else
            if(myComponent instanceof WidgetTextArea)
                ((WidgetTextArea)myComponent).initializeDrop();
    }

    public synchronized void triggerEvent(String actionType)
    {
        ActionElement action = (ActionElement)actions.get(actionType);
        if(action != null)
            try
            {
                String script = action.getScript();
                if(script != null)
                    widget.executeScript(widget, action.getScript(), name + ":" + actionType);
                else
                if(action.getFile() != null)
                    widget.executeScriptFile(widget, action.getScript());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }

    private String cursor;
    public Cursor cursorObj;
    protected int height;
    protected String name;
    private int opacity;
    private Object owner;
    protected int width;
    private String tooltip;
    private boolean visible;
    protected int x;
    protected int y;
    public JComponent myComponent;
    protected Hashtable actions;
}